﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SynoManager.FileStation.Tasks;

namespace SynoManager.FileStation.Gui
{
    public partial class TaskQueueList : UserControl
    {
        public TaskQueueList()
        {
            InitializeComponent();
        }

        private TaskQueue queue;
        [EditorBrowsable(EditorBrowsableState.Never)]
        public TaskQueue TaskQueue
        {
            get { return queue;}
            set
            {
                queue = value;
                listBox.DataSource = queue;
                if (queue != null)
                {
                    queue.ListChanged += new ListChangedEventHandler(queue_ListChanged);
                }
            }
        }

        void queue_ListChanged(object sender, ListChangedEventArgs e)
        {
            if (listBox.InvokeRequired)
            {
                listBox.Invoke(new ListChangedEventHandler(queue_ListChanged), sender, e);
                return;
            }
            listBox.Update();
        }

        private void listBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();

            Graphics g = e.Graphics;
            if (e.Index >= 0)
            {
                TaskBase task = (TaskBase) listBox.Items[e.Index];

                Font boldFont = new Font(Font, FontStyle.Bold);
                StringFormat alignRight = ((StringFormat) StringFormat.GenericDefault.Clone());
                alignRight.Alignment = StringAlignment.Far;

                // print description
                g.DrawString(task.DisplayTitle, boldFont, Brushes.Black, e.Bounds.X + 20, e.Bounds.Y + 3);
                // print image
                g.DrawImage(task.DisplayImage, e.Bounds.X + 2, e.Bounds.Y + 2);
                // print state
                g.DrawString(task.State.ToString(), boldFont, Brushes.Black,
                             new RectangleF(e.Bounds.X + 3, e.Bounds.Y + 3, e.Bounds.Width - 4, 20), alignRight);

                g.DrawString(task.DisplayDescription, Font, Brushes.Black, e.Bounds.X + 20, e.Bounds.Y+22);

                int progress = (int)(100d*task.Progress);

                g.DrawRectangle(Pens.Black, e.Bounds.Right - 106, e.Bounds.Y + 22, 104, 13);
                g.FillRectangle(Brushes.Lime, e.Bounds.Right - 104, e.Bounds.Y + 24, progress, 10);
            }

            g.DrawLine(Pens.Gray, e.Bounds.Left, e.Bounds.Bottom, e.Bounds.Right, e.Bounds.Bottom);

            e.DrawFocusRectangle();
        }
    }
}
